<?php
/**
 * Object Relational Persistence Mapping Class
 * for table: test_diagnosis_ticket.
 * @package core.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class TestDiagnosisTicket extends ORDataObject {

	/**#@+
	 * Fields of table: test_diagnosis_ticket mapped to class members
	 */
	var $test_diagnosis_ticket_id		= '';
	var $requested_time		= '';
	var $diagnosis		= '';
	var $ticket_id		= '';
	var $doctor_id		= '';
	var $is_done		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'test_diagnosis_ticket';

	/**
	 * Primary Key
	 */
	var $_key = 'test_diagnosis_ticket_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'TestDiagnosisTicket';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}

	/**
	 * Convert null number to displayable value (-1) -> ''
	 * @param $vitalSign
	 * @return nothing.
	 */
	public static function normalizeData4Display(&$data) {
		if (is_array($data)) {
			$data['requested_time'] = L10nHelper::renderDateTime($data['requested_time'],'short','datetime');
		}
	}


	/**
	 * Get the list of assigned test ticket.
	 */
	public function listOfAssignedTestTicket($ticketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "test_diagnosis_ticket_id, requested_time, diagnosis, PE.name doctor_name, doctor_id, is_done",
            'from'  => "$this->_table t LEFT JOIN person PE ON (t.doctor_id > 0 AND t.doctor_id = PE.person_id)", 
            'where' => "ticket_id = '" . $ticketId ."'",
            'orderby' => 'requested_time DESC'
        
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            return $result;
	}

	/**
	 * Get Assigned test ticket.
	 * return array (test_diagnosis_ticket_id, requested_time, diagnosis, PE.name doctor_name, doctor_id, is_done)
	 */
	public function getAssignedTestTicket($testDiagnosisTicketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "test_diagnosis_ticket_id, requested_time, diagnosis, PE.name doctor_name, doctor_id, is_done",
            'from'  => "$this->_table t LEFT JOIN person PE ON (t.doctor_id > 0 AND t.doctor_id = PE.person_id)", 
            'where' => "test_diagnosis_ticket_id = '" . $testDiagnosisTicketId ."'"
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            if (empty($result)) {
            	return null;
            }
            return $result[0];
	}
	public function isTestTicketExist($testDiagnosisTicketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "test_diagnosis_ticket_id",
            'from'  => "$this->_table", 
            'where' => "test_diagnosis_ticket_id = '" . $testDiagnosisTicketId ."'"
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            if (empty($result)) {
            	return false;
            }
            return true;
	}
	/**
	 * create new object for every invoke this function.
	 * @param $testTicketInfo
	 * array(ticket_id,requested_time,diagnosis,ticket_id,doctor_id, is_done)
	 * @param $assignedTest
	 * array([test_service_id],...)
	 * @return id of added test ticket.
	 */
	public function addAssignedTest($testTicketInfo, $assignedTest) {
		//add new test ticket
		$this->populateArray($testTicketInfo);
		$this->persist();
		$testDiagnosisTicketId = $this->get_id();

		$serviceObj = Celini::newOrdo('Service');
		$testServiceObj = Celini::newOrdo('TestService');
		$testResultObj = Celini::newOrdo('AssigedTestItem');
		$testResultObj->removeAssignedTestItem($testDiagnosisTicketId);
		foreach ($assignedTest  as $testSerivceId) {
			$service = $testServiceObj->getService($testSerivceId);
			if ($service != null) {

				$prices = $serviceObj->getPrices($testTicketInfo['ticket_id'], $service['service_id'], EnumerationConstant::SER_INS_ITEM_TYPE_LAB);

				$data = array('test_service_id'=>$testSerivceId,
                        'test_diagnosis_ticket_id'=>$testDiagnosisTicketId,
                        'original_price'=>$prices['price'],
                        'insurance_price'=>$prices['insurance_price'],
			            'is_done'=>0);
				$testResultObj = Celini::newOrdo('AssigedTestItem');
				$testResultObj->addAssignedTestItem($data);
			}
		}
		return $testDiagnosisTicketId;
	}

	public function editAssignedTest($testDiagnosisTicketId, $testTicketInfo, $assignedTest) {
		//add new test ticket
		if ($this->isTestTicketExist($testDiagnosisTicketId) == false) {
			return;
		}

		$this->setup($testDiagnosisTicketId);
		$this->populateArray($testTicketInfo);
		$this->persist();
		$serviceObj = Celini::newOrdo('Service');
		$testServiceObj = Celini::newOrdo('TestService');
		$testResultObj = Celini::newOrdo('AssigedTestItem');
		$testResultObj->removeAssignedTestItem($testDiagnosisTicketId);
		foreach ($assignedTest  as $testSerivceId) {
			$service = $testServiceObj->getService($testSerivceId);
			if ($service != null) {

				$prices = $serviceObj->getPrices($testTicketInfo['ticket_id'], $service['service_id'], EnumerationConstant::SER_INS_ITEM_TYPE_LAB);

				$data = array('test_service_id'=>$testSerivceId,
                        'test_diagnosis_ticket_id'=>$testDiagnosisTicketId,
                        'original_price'=>$prices['price'],
                        'insurance_price'=>$prices['insurance_price'],
                        'is_done'=>0);
				$testResultObj = Celini::newOrdo('AssigedTestItem');
				$testResultObj->addAssignedTestItem($data);
			}
		}
		return $testDiagnosisTicketId;

	}

	/**
	 *
	 * @param $ticketId
	 * @param $testTicketInfo
	 * array(requested_time,diagnosis,ticket_id,doctor_id, is_done)
	 * @param $assignedTest
	 * array(test_service_id)
	 * @return unknown_type
	 */
	public function updateAssignedTest($testDiagnosisTicketId, $testTicketInfo, $assignedTest) {

	}

	public function deleteTestTicket($testTicketId) {
		$sql = "DELETE FROM assiged_test_item WHERE test_diagnosis_ticket_id ='" . $testTicketId . "'";
		$this->_execute($sql);
		$sql = "DELETE FROM $this->_table WHERE test_diagnosis_ticket_id ='" . $testTicketId . "'";
		$this->_execute($sql);
	}

	public function setDone($testDiagnosisTicketId, $done) {
		$sql = "UPDATE $this->_table SET is_done = '$done' WHERE test_diagnosis_ticket_id = '$testDiagnosisTicketId'";
		$this->_execute($sql);
	}
	public function canDeleteTestTicket($testDiagnosisTicketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "test_diagnosis_ticket_id",
            'from'  => "$this->_table", 
            'where' => "test_diagnosis_ticket_id = '" . $testDiagnosisTicketId ."' AND is_done='1'"
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            if (empty($result)) {
            	return true;
            }
            return false;
	}

	public function isTestTicketDone($testDiagnosisTicketId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "test_diagnosis_ticket_id",
            'from'  => "$this->_table", 
            'where' => "test_diagnosis_ticket_id = '" . $testDiagnosisTicketId ."' AND is_done='1'"
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            if (empty($result)) {
            	return false;
            }
            return true;
	}




}
?>
